package com.wc.service.impl;

import com.wc.dao.EmpDao;
import com.wc.dao.impl.EmpDaoImpl;
import com.wc.domain.Dept;
import com.wc.domain.Emp;
import com.wc.meiju.Role;
import com.wc.service.EmpService;
import com.wc.utils.Md5Util;

/**
 * @author wanghao
 */
public class EmpServiceImpl implements EmpService {
    private EmpDao empDao = new EmpDaoImpl();

    @Override
    public int login(String account, String password) {
        int empno = empDao.selectEmpnoByAccount(account);
        if (empno == -1) {
            return -1;
        }
        Emp emp = empDao.selectEmpByEmpno(empno);
        String encode = Md5Util.encode(password, emp.getSalt());
        if (emp.getPassword().equals(encode)) {
            return empno;
        }
        return -1;
    }

    @Override
    public Emp getEmpByEmpno(Integer empno) {
        Emp emp = empDao.selectEmpByEmpno(empno);
        return emp;
    }

    @Override
    public Role getRoleByEmpno(Integer empno) {
        Emp emp = getEmpByEmpno(empno);
        if (emp != null) {
            return emp.getRole();
        }
        return null;
    }


    @Override
    public String updateEmp(Emp emp) {
        boolean res = empDao.updateEmp(emp);
        return res ? "修改成功" : "修改失败";
    }

    @Override
    public Dept getDeptByEmpno(Integer empno) {
        Dept dept = empDao.selectDeptByEmpno(empno);
        return dept;
    }
}